<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use DB;
use App\Http\Requests;
use App\Http\Controllers\Controller;

class IndexController extends Controller
{
    /**
     * 前台首页
     */
 
     
    public function getIndex(Request $request)
    {
      //明星榜点击量
		$goods0 = DB::table('goods')
            ->join('kucun','goods.id','=','kucun.gid')
             ->where('zome','0')
            ->groupBy('goods.id')
		        ->get();

// dd($goods1);

    //硬件专区
		$goods1 = DB::table('goods')
            ->join('kucun','goods.id','=','kucun.gid')
             ->where('zome','1')
            ->groupBy('goods.id')
            ->get();
    //配件专区
    $goods2 = DB::table('goods')
            ->join('kucun','goods.id','=','kucun.gid')
             ->where('zome','2')
            ->groupBy('goods.id')
            ->get();
    // 轮播图
    $carousel = DB::table('cimgs')
                ->where('delcimgs','x')
                ->get();  

    //其他专区 未填写




    //左边导航栏
         $cates=CateController::getCateArr(CateController::getAllCate(),0);
             //dd($cates);
      
       foreach ($cates as $k => $v) {
            $vv = $v->id;

            $goods = DB::table('goods')
            ->join('cates','cates.id','=','goods.cateid')
            ->where('cates.id',$vv)->select('goods.*')->get();
            
           
           $cates[$k]->goods= $goods;
           }

      // dd($cates);

         //首页购物车  通过 用户id
       $u_id = session('id');
       if(!empty($u_id)){
        
       $cart = DB::table('cart')->where('u_id',$u_id)->count();

       $cartgoods = DB::table('cart')->join('goods','goods.id','=','cart.g_id')->where('u_id',$u_id)->get();
        //dd($cartgoods);

       }else{

          $cart = 0;
          $cartgoods = [];
       }

       
    	return view('home.index',['cartgoods'=>$cartgoods,'cart'=>$cart,'cates'=>$cates,'goods0'=>$goods0,'goods1'=>$goods1,'goods2'=>$goods2,'cimgs'=>$carousel]);


    
     //  $cates=CateController::getCateArr(CateController::getAllCate(),0);
     //  	//dd($cates);

    	// return view('home.index',['cates'=>$cates,'goods1'=>$goods1,'goods4'=>$goods4,'goods5'=>$goods5,'cimgs'=>$carousel]);

    }


    // 商品详情页

    public function getDefault($id)
    {
     
      //dd($id);
      //查询商品信息
      $default = DB::table('goods')->where("id",$id)->first();
      $default1 = DB::table('kucun')->where('gid',$id)->first();
      $default3 = DB::table('kucun')->where('gid',$id)->get();
      $default2 = DB::table('guige')->where('gid',$id)->get();

      //dd($default2);

      $cateid = $default->cateid;
      
      $catepid = DB::table('cates')->where('id',$cateid)->first();

      $pid1 = $catepid->pid;
       //dd($pid1);
      $pid = DB::table('cates')->where('pid',$pid1)->get();
      
   //dd($pid);
      //商品的套餐
        //商品id对应的套餐里面的sgsid 
        $res = DB::table('goods')
              ->join('package', 'goods.id', '=','package.sgsid')
             
             
              ->where('goods.id',$id)
              ->get();
              //dd($res);
      //dd($res);
      $tcsp = DB::table('goods')
              ->leftJoin('package', 'goods.id', '=','package.sgsid')
              ->join('packagegoods','package.id', '=' ,'packagegoods.peid')
              ->select('package.id as package_id','package.*','packagegoods.*','goods.*')
              ->where('goods.id',$id)
              ->get();
      //dd($tcsp);

   //    return $res;

        //查询套餐的图片 通过ajax发送id查询套餐中商品的图片


      return view('home.default',['default'=>$default,'default1'=>$default1,'default2'=>$default2,'default3'=>$default3,'pid'=>$pid,'res'=>$res,'tcsp'=>$tcsp]);

    }
    
    //接受ajax 数据 处理发来的库存id 规格id 套餐 id

    public function getAa(Request $request)
    {
      // $data = $request->session()->all();
      // dd($data);
      $data = $request->all();
      //$data = session($data);
      //dd($data);
       // 商品 id
       $g_id = $data['g_id'];
      //库存的型号
       $size = $data['size'];
      //规格的id
      $color = $data['color'];
      //套餐的id
      $package_id = $data['pk'];
      //查到商品的价格
      $price = $data['price'];

      $num = 1;
        //dd($goods_id);
      // 把商品详情页所选的商品存入session 如果此时检测用户id不存在就将数据存入cart数据库中此时商品默认为一件
      // 查询数据库中;如果此时检测用户id存在就将数据存入cart数据库中
        $u_id = session('id');
        if($u_id != null)
          //存在就把添加的商品信息存入数据库
        {
          $res_cart = DB::table('cart')->where('g_id',$g_id)->get();
              if($res_cart == null){

                  $cartinfo = ['u_id'=>$u_id, 'g_id'=>$g_id,'size'=>$size,'color'=>$color,'package_id'=>$package_id, 'price'=>$price,'num'=>$num];

                  $res = DB::table('cart')->insert($cartinfo);
            //dd($res);
              }
        }




      $res = ['g_id'=>$g_id,'size'=>$size,'color'=>$color,'package_id'=>$package_id, 'price'=>$price, 'num'=>$num];

      //dd($res);

         $gd = session('cart');

         //dd($gd);
          if(empty($gd)){

              session()->push('cart', $res);
           //echo "string";
          }else{

              foreach ($gd as $k => $v) {
                   //var_dump($v['id']);
                 if($v['g_id'] == $g_id){
                  //echo "string";
                 die;
              }
               

            

         }
          // session('cart', $res);
         session()->push('cart', $res);
       }
      
      //session()->push('cart', $res);


    }

    




}
